<template>
    <el-dialog v-model="editData.visible" :title="editData.title" center width="60%" top="10vh" :close-on-click-modal="false">
        <div class="dialog-content">
            <el-form :model="editData.form" label-width="auto" style="width:100%" ref="formRef" :rules="editData.rules">
                <el-row :gutter="50">

                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                        <el-form-item label="图片素材" prop="orderNum">
                            <upload-img-video v-model="editData.form.resources" :aspect-ratio="5/4" mode="multiple"></upload-img-video>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                        <el-form-item label="素材排序" prop="orderNum">
                            <el-input-number min="0" v-model="editData.form.orderNum" type="number" placeholder="请输入排序" style="width:100%"/>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                        <el-form-item label="审核状态" prop="status">
                            <el-switch v-model="editData.form.status" :active-value="1" :inactive-value="0"></el-switch>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="editData.visible = false">取消</el-button>
                <el-button type="primary" @click="submitForm">确定</el-button>
            </div>
        </template>
    </el-dialog>

</template>


<script setup>
import { nextTick, reactive, useTemplateRef } from 'vue'
import { ElMessage } from 'element-plus'
import motionMaterialApi from 'api/model/motion-material.js'
import UploadImgVideo from '@/components/comp-upload/upload-img-video.vue'

defineExpose({ openDialog })

const props = defineProps({
    motionId: { type: String, default: '' }
})

const emits = defineEmits(['update'])
const formRef = useTemplateRef('formRef')

// 编辑相关数据
const editData = reactive({
    type: 'add',
    visible: false,
    form: {
        id: '',
        orderNum: '',
        resources: [],
        status: 1,
    },
    rules: {}
})

/**
 * 打开弹框
 * @param type
 * @param item
 */
function openDialog (type, item) {
    editData.title = type === 'add' ? '新增动作素材' : '编辑动作素材'
    editData.visible = true
    editData.type = type
    editData.form.id = ''
    editData.form.resources =  []
    nextTick(() => {
        formRef.value.resetFields()
        if (type === 'update') {
            const { id, orderNum, resources, status } = item || {}
            editData.form.id = id
            editData.form.orderNum = orderNum
            editData.form.resources = resources
            editData.form.status = status
        }
    })
}

/**
 * 提交表单
 */
function submitForm () {
    formRef.value.validate(async valid => {
        if (!valid) return
        editData.type === 'add' ? await addItem() : await updateItem()
    })
}

/**
 *  新增操作
 */
async function addItem () {
    const { code } = await motionMaterialApi.add.post({
        motionId: props.motionId,
        resources: editData.form.resources,
        status: editData.form.status || 0,
        orderNum: editData.form.orderNum,
    })
    if (code === 200) {
        ElMessage.success('新增成功')
        editData.visible = false
        emits('update')
    } else {
        ElMessage.error('新增失败')
    }
}

/**
 *  更新操作
 */
async function updateItem () {
    const { code } = await motionMaterialApi.update.post({
        id: editData.form.id,
        resources: editData.form.resources,
        status: editData.form.status || 0,
        orderNum: editData.form.orderNum,
    })
    if (code === 200) {
        ElMessage.success('编辑成功')
        editData.visible = false
        emits('update')
    } else {
        ElMessage.error('编辑失败')
    }
}


</script>

<style lang="less">
.dialog-content {
    max-height: 70vh;
    overflow-y: auto;
    overflow-x: hidden;
    box-sizing: border-box;
}
</style>
